package demo.controller;

import demo.Result;
import demo.dto.RequestListPMsDto;
import demo.dto.RequestSendPMDto;
import demo.dto.RequestWithLongDto;
import demo.dto.ResponseContactsDto;
import demo.entity.PM;
import demo.entity.User;
import demo.service.PMService;
import demo.service.UserService;
import demo.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 私信管理 Controller
 *
 * @author 肖溥博文
 * @version 1.1
 */
@RestController
public class PMController {

    @Autowired
    PMService pmService;
    @Autowired
    UserService userService;

    /**
     * 发送私信
     *
     * @param requestSendPMDto 必须包含发送人 uid，接收人 uid，私信内容
     * @return 结果对象，code 200：发送成功，code 400：发送失败
     */
    @PostMapping("/sendPM")
    @RequiresAuthentication
    public Result sendPM(@Validated @RequestBody RequestSendPMDto requestSendPMDto) {
        return pmService.sendPM(requestSendPMDto.getSenderUid(), requestSendPMDto.getRecipientUid(), requestSendPMDto.getContent());
    }

    /**
     * 查询 A 单向发送给 B 的所有私信
     *
     * @param requestListPMsDto 必须包含发送人 uid 和接收人 uid
     * @return 结果对象
     */
    @PostMapping("/listPMsOneSide")
    @RequiresAuthentication
    public Result listPMsOneSide(@Validated @RequestBody RequestListPMsDto requestListPMsDto) {
        return pmService.listPMsOneSide(requestListPMsDto.getSenderUid(), requestListPMsDto.getRecipientUid());
    }

    /**
     * 查询 A 和 B 之间的所有私信
     *
     * @param requestListPMsDto 必须包含双方 uid（顺序可颠倒）
     * @return 结果对象
     */
    @PostMapping("/listPMs")
    @RequiresAuthentication
    public Result listPMs(@Validated @RequestBody RequestListPMsDto requestListPMsDto) {
        return pmService.listPMs(requestListPMsDto.getSenderUid(), requestListPMsDto.getRecipientUid());
    }

    /**
     * 按 uid 获取消息列表
     *
     * @param requestWithLongDto 必须包含一个 uid
     * @return 消息列表数据，以数组形式返回。包含最后联系时间、最后联系内容、联系人、私信 id
     */
    @PostMapping("/listContacts")
    @RequiresAuthentication
    public Result listContacts(@Validated @RequestBody RequestWithLongDto requestWithLongDto) {
        List<ResponseContactsDto> responseContactsDtos = new ArrayList<>();
        Long uid = requestWithLongDto.getId();
        List<Long> ids = pmService.listLatestContactPMIdByUid(uid);
        List<Long> contactIds = pmService.listLatestContactUidsByUid(uid);
        for (int i = 0; i < ids.size(); i++) {
            ResponseContactsDto responseContactsDto = new ResponseContactsDto();
            PM pm = pmService.getPMById(ids.get(i));
            responseContactsDto.setDate(DateUtil.format(pm.getSendingTime()));
            responseContactsDto.setUid(contactIds.get(i));
            User user = userService.findUserByUid(contactIds.get(i));
            responseContactsDto.setUserName(user.getUserName());
            responseContactsDto.setPMId(ids.get(i));
            responseContactsDto.setContent(pm.getContent());
            responseContactsDtos.add(responseContactsDto);
        }
        return Result.success("查询消息列表成功", responseContactsDtos);
    }
}
